package com.www.applyword.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.DynamicAdapter;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.DynaminBean;
import com.www.applyword.bean.HotJobBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.chat.DynamicDetailsActivity;
import com.www.applyword.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/11/9 0009 14:31
 * version: 1.0
 */
public class AreaDynaminFragment extends BaseFragment {
    @BindView(R.id.rv_dynamic)
    RecyclerView rvDynamic;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;
    @BindView(R.id.header)
    ClassicsHeader header;
    private DynamicAdapter mDynamicAdapter;
    private List<HotJobBean> hotJobBeans = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;
    private List<DynaminBean.ListBean> list = new ArrayList<>();
    ViewStub vsEmpty;

    @Override
    protected int getLayoutResource() {
        return R.layout.friend_dynamin_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        vsEmpty =  rootView.findViewById(R.id.vs_empty);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initView();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.FANBU)) {
                getForumList(true);
            }
        }
    }


    private void initView() {
        rvDynamic.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvDynamic.setItemAnimator(new DefaultItemAnimator());
        mDynamicAdapter = new DynamicAdapter(getActivity());
        rvDynamic.setAdapter(mDynamicAdapter);

        refresh.setOnRefreshListener(refreshLayout -> getForumList(true));

        refresh.setOnLoadMoreListener(refreshLayout ->getForumList(false));

        getForumList(true);
    }

    private void getForumList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        params.put("Address", SPUtil.getString("city"));
        params.put("UserID", SPUtil.getString(Constants.USERID));
        HttpClient.get(getActivity(), Constants.GETFORUMLIST, params,new CallBack<DynaminBean>() {
            @Override
            public void onSuccess(DynaminBean result) {
                list.clear();
                list.addAll(result.getList());
                if (isRefresh){
                    refresh.finishRefresh(true);
                    if (list.size() == 0){
                        showEmpty(true,vsEmpty,2);
                    }else {
                        showEmpty(false, vsEmpty,2);
                    }
                    mDynamicAdapter.replaceAll(list, true);
                }else {
                    if (list.size() > 0) {
                        refresh.finishLoadMore(true);
                        mDynamicAdapter.addAll(list);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });

        mDynamicAdapter.setOnItemClickListener((viewHolder, view, i) ->
                startActivity(new Intent(getActivity(), DynamicDetailsActivity.class).putExtra("ForumID", mDynamicAdapter.getItem(i).getForum_ID())));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
